<template>
  <div class="vehicle-settings-page">
    <van-nav-bar title="车辆设置" left-text="返回" left-arrow @click-left="router.back()" fixed />
    
    <van-empty v-if="!vinNo" description="缺少 vinNo 参数" />

    <div v-else>
      <van-skeleton title :row="6" v-if="loading" />

      <template v-else>
        <van-cell-group inset title="基础设置">
          <van-cell title="最高速度" :value="`${info.topSpeed || '-'} km/h`" />
          <van-cell title="低电量阈值" :value="`${info.lowPower ?? '-'}%`" />
          <van-cell title="里程单位" :value="formatMileageUnit(info.mileageUnit)" />
          <van-cell title="温度单位" :value="formatTemperatureUnit(info.temperatureUnit)" />
          <van-cell title="时间格式" :value="formatTimeFormat(info.timeFormat)" />
          <van-cell title="语言" :value="formatLanguage(info.language)" />
          <van-cell title="离车关机(秒)" :value="info.leaveOffTime ?? '-'" />
          <van-cell title="主题" :value="`${info.themeName || '-'}(#${info.themeId || '-'})`" />
          <van-cell title="充电功率" :value="info.chargingPower ? info.chargingPower + ' W' : '-'" />
        </van-cell-group>

        <van-cell-group inset title="功能开关">
          <van-cell title="MotoPlay" :value="onOff(info.motoPlay)" />
          <van-cell title="牵引力控制(TCS)" :value="onOff(info.tcsTractionControl ?? info.etcsTractionControl)" />
          <van-cell title="ABS" :value="onOff(info.antiLockBrakeSystem)" />
          <van-cell title="自动关机" :value="onOff(info.automaticShutdown)" />
          <van-cell title="自动关锁" :value="onOff(info.automaticLock)" />
          <van-cell title="巡航控制功能" :value="onOff(info.cruiseControlFunction)" />
          <van-cell title="感应解锁" :value="onOff(info.inductiveUnlocking)" />
          <van-cell title="能量回收" :value="onOff(info.kineticEnergyRecovery)" />
          <van-cell title="自动驻车" :value="onOff(info.automaticParking)" />
          <van-cell title="靠边支架感应" :value="onOff(info.edgeSupportSensing)" />
          <van-cell title="座垫感应" :value="onOff(info.cushionSensor ?? info.ecushionSensor)" />
          <van-cell title="扩展续航" :value="onOff(info.extendedRange)" />
          <van-cell title="回家伴你(延时)" :value="info.comeHomeWithYou ? info.comeHomeWithYouTime + ' s' : '关闭'" />
        </van-cell-group>

        <van-cell-group inset title="巡航设置">
          <van-cell title="巡航模式" :value="formatCruiseAcc(info.cruiseMode)" />
          <van-cell title="巡航保持" :value="onOff(info.cruiseControlHold)" />
          <van-cell title="巡航ACC" :value="formatCruiseAcc(info.cruiseAcc)" />
        </van-cell-group>

        <van-cell-group inset title="声效设置">
          <van-cell title="总开关" :value="onOff(info.soundEffectSetting?.switchAll)" />
          <van-cell title="低速声效" :value="onOff(info.soundEffectSetting?.switchLowVelocity)" />
          <van-cell title="驻车声效" :value="onOff(info.soundEffectSetting?.switchStand)" />
          <van-cell title="骑行模式切换" :value="onOff(info.soundEffectSetting?.switchRidingModel)" />
          <van-cell title="坐下声效" :value="onOff(info.soundEffectSetting?.switchSitDown)" />
          <van-cell title="Ready声效" :value="onOff(info.soundEffectSetting?.switchReady)" />
          <van-cell title="关机声效" :value="onOff(info.soundEffectSetting?.switchShutdown)" />
          <van-cell title="定速巡航提示" :value="onOff(info.soundEffectSetting?.switchCruiseControl)" />
          <van-cell title="音量" :value="(info.soundEffectSetting?.soundSize ?? '-') + '%'" />
          <van-cell title="解锁/上锁声" :value="onOff(info.soundEffectSetting?.unlockLock)" />
          <van-cell title="模式切换声" :value="onOff(info.soundEffectSetting?.modeSwitching)" />
          <van-cell title="静态操作声" :value="onOff(info.soundEffectSetting?.staticOperation)" />
          <van-cell title="防盗报警声" :value="onOff(info.soundEffectSetting?.burglarAlarm)" />
          <van-cell title="转向提示音" :value="onOff(info.soundEffectSetting?.cyclingSteering)" />
          <van-cell title="雷达声效" :value="onOff(info.soundEffectSetting?.radarSound)" />
        </van-cell-group>

        <van-cell-group inset title="灯光设置">
          <van-cell title="停车动态灯效" :value="onOff(info.lightEffectSetting?.parkingDynamicEffect)" />
          <van-cell title="骑行动态灯效" :value="onOff(info.lightEffectSetting?.cyclingDynamicEffect)" />
          <van-cell title="近光灯" :value="onOff(info.lightEffectSetting?.lowBeamLight)" />
          <van-cell title="示宽灯" :value="onOff(info.lightEffectSetting?.positionLight)" />
          <van-cell title="充电动态灯效" :value="formatChargingEffect(info.lightEffectSetting?.chargingDynamicEffect)" />
          <van-cell title="长距离照明" :value="onOff(info.lightEffectSetting?.longDistanceLighting)" />
          <van-cell title="ECO颜色" :value="formatColor(info.lightEffectSetting?.ecoColor)" />
          <van-cell title="STREET颜色" :value="formatColor(info.lightEffectSetting?.streetColor)" />
          <van-cell title="SPORT颜色" :value="formatColor(info.lightEffectSetting?.sportColor)" />
        </van-cell-group>

        <van-cell-group inset title="雷达与安全">
          <van-cell title="BSD盲点监测" :value="formatCruiseAcc(info.radarBsd)" />
          <van-cell title="RCW后碰撞预警" :value="formatCruiseAcc(info.radarRcw)" />
          <van-cell title="FCW前碰撞预警" :value="formatCruiseAcc(info.radarFcw)" />
          <van-cell title="紧急制动双闪" :value="formatCruiseAcc(info.emergencyBrakeFlash)" />
          <van-cell title="陡坡缓降" :value="formatCruiseAcc(info.steepSlopeDescent)" />
        </van-cell-group>

        <van-cell-group inset title="自定义按键">
          <van-cell title="左按键" :value="formatCustom(info.customButtonLeft)" />
          <van-cell title="右按键" :value="formatCustom(info.customButtonRight)" />
          <van-cell title="组合键" :value="formatCustom(info.customButtonCombination)" />
          <van-cell title="单按键" :value="formatCustom(info.customButtonSingle)" />
        </van-cell-group>

        <div class="footer-tips">以上为只读展示，后期可支持修改</div>
      </template>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { showFailToast } from 'vant';
import { getVehicleSettingsInfo } from '@/api/vehicle';

const router = useRouter();
const route = useRoute();
const vinNo = route.query?.vinNo || '';
const loading = ref(true);
const info = ref({});

const fetchData = async () => {
  if (!vinNo) return;
  try {
    const res = await getVehicleSettingsInfo(vinNo);
    if (res?.code === '10000') {
      info.value = res.data || {};
    } else {
      showFailToast(res?.message || '获取设置失败');
    }
  } catch (e) {
    showFailToast(e?.response?.data?.message || e?.message || '网络错误');
  } finally {
    loading.value = false;
  }
};

onMounted(() => {
  fetchData();
});

const onOff = (v) => (Number(v) === 1 || v === true ? '开启' : '关闭');
const formatMileageUnit = (v) => (String(v) === '1' ? 'km' : 'mile');
const formatTemperatureUnit = (v) => (String(v) === '1' ? '°C' : '°F');
const formatTimeFormat = (v) => (String(v) === '1' ? '24小时制' : '12小时制');
const formatLanguage = (v) => {
  const map = { '1': '中文', '2': '英文' };
  return map[String(v)] || '-';
};
const formatCruiseMode = (v) => {
  const map = { '1': '标准', '2': '智能', '3': '自适应' };
  return map[String(v)] || '-';
};
const formatCruiseAcc = (v) => {
  const map = { '0': '关闭', '1': '标准', '2': '强烈' };
  return map[String(v)] || '-';
};
const formatColor = (v) => {
  const map = { 1: '红', 2: '橙', 3: '黄', 4: '绿', 5: '青', 6: '蓝', 7: '紫' };
  return map[Number(v)] || '-';
};
const formatChargingEffect = (v) => {
  const map = { 0: '关闭', 1: '模式一', 2: '模式二', 3: '模式三' };
  return map[Number(v)] ?? '-';
};
const formatLevel = (v) => {
  const map = { 0: '关闭', 1: '低', 2: '中', 3: '高' };
  return map[Number(v)] ?? '-';
};
const formatCustom = (v) => `功能#${v ?? '-'}`;
</script>

<style scoped>
.vehicle-settings-page {
  padding-top: 45px;
  height: 100%;
}


.title {
  font-size: 20px;
  font-weight: 600;
}

.sub {
  color: #666;
  font-size: 12px;
}

.footer-tips {
  margin: 16px 12px;
  color: #888;
  font-size: 12px;
  text-align: center;
}
</style>
